package com.example.service.impl;

import com.example.pojo.Order;
import com.example.pojo.Product;
import com.example.service.OrderService;
import com.example.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author yinyin
 * @create 2021/07/14 下午 3:28
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ProductService productService;
    @Override
    public Order selectOne(Integer id) {


        return new Order(id,"order-003","中国",29000D,
                Arrays.asList(productService.selectProductById(5)));
    }

  /*请求合并测试
   @Override
    public Order selectOne(Integer id) {
        // return new Order(id,"order-001","中国",2788D, Arrays.asList(productService.selectProductById(id)));
        //模拟同一时间用户发起多个请求
        Future<Product> p1= productService.selectProductById(1);
        Future<Product> p2= productService.selectProductById(2);
        Future<Product> p3= productService.selectProductById(3);
        Future<Product> p4= productService.selectProductById(4);
        Future<Product> p5= productService.selectProductById(5);
        try {
            System.out.println(p1.get());
            System.out.println(p2.get());
            System.out.println(p3.get());
            System.out.println(p4.get());
            System.out.println(p5.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return new Order(id,"order-003","中国",29000D,Arrays.asList(new Product()));
    }*/

    @Override
    public Order queryOne(Integer id) {
        return new Order(id,"order-002","中国",11660D,productService.selectProductList());
    }

    @Override
    public Order searchOne(Integer id) {
        return new Order(id,"order--003","中国",2266D, productService.selectProductListByIds(Arrays.asList(1,2)));
    }
}
